package com.example.managemyself.util;

import android.app.DatePickerDialog;
import android.content.Context;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 全局日期管理器
 * 用于管理应用中的日期选择和数据同步
 */
public class DateManager {
    
    private static DateManager instance;
    private final MutableLiveData<Date> selectedDate = new MutableLiveData<>();
    private List<DateChangeListener> listeners;
    private DateChangeListener primaryListener;
    
    public DateManager(DateChangeListener primaryListener) {
        this.primaryListener = primaryListener;
        selectedDate.setValue(new Date()); // 默认为今天
        listeners = new ArrayList<>();
        if (primaryListener != null) {
            listeners.add(primaryListener);
        }
    }
    
    public static synchronized DateManager getInstance() {
        if (instance == null) {
            instance = new DateManager(null);
        }
        return instance;
    }
    
    /**
     * 显示日期选择器
     */
    public void showDatePicker(Context context) {
        Calendar calendar = Calendar.getInstance();
        Date currentSelected = selectedDate.getValue();
        if (currentSelected != null) {
            calendar.setTime(currentSelected);
        }
        
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        
        DatePickerDialog datePickerDialog = new DatePickerDialog(
            context,
            (view, selectedYear, selectedMonth, selectedDay) -> {
                Calendar newCalendar = Calendar.getInstance();
                newCalendar.set(selectedYear, selectedMonth, selectedDay, 0, 0, 0);
                newCalendar.set(Calendar.MILLISECOND, 0);
                
                Date newDate = newCalendar.getTime();
                setSelectedDate(newDate);
            },
            year, month, day
        );
        
        datePickerDialog.show();
    }
    
    /**
     * 设置选择的日期
     */
    public void setSelectedDate(Date date) {
        selectedDate.setValue(date);
        notifyListeners();
    }
    
    /**
     * 获取当前选择的日期
     */
    public Date getSelectedDate() {
        return selectedDate.getValue();
    }
    
    /**
     * 获取选择日期的LiveData，供其他组件观察
     */
    public LiveData<Date> getSelectedDateLiveData() {
        return selectedDate;
    }
    
    /**
     * 检查选择的日期是否是今天
     */
    public boolean isToday() {
        Calendar today = Calendar.getInstance();
        Calendar selected = Calendar.getInstance();
        selected.setTime(selectedDate.getValue());
        
        return today.get(Calendar.YEAR) == selected.get(Calendar.YEAR) &&
               today.get(Calendar.DAY_OF_YEAR) == selected.get(Calendar.DAY_OF_YEAR);
    }
    
    /**
     * 重置到今天
     */
    public void resetToToday() {
        setSelectedDate(new Date());
    }
    
    /**
     * 添加日期变化监听器
     */
    public void addDateChangeListener(DateChangeListener listener) {
        if (listener != null && !listeners.contains(listener)) {
            listeners.add(listener);
        }
    }
    
    /**
     * 移除日期变化监听器
     */
    public void removeDateChangeListener(DateChangeListener listener) {
        listeners.remove(listener);
    }
    
    /**
     * 通知所有监听器日期已变化
     */
    private void notifyListeners() {
        for (DateChangeListener listener : listeners) {
            listener.onDateChanged(selectedDate.getValue());
        }
    }
    
    /**
     * 日期变化监听器接口
     */
    public interface DateChangeListener {
        void onDateChanged(Date newDate);
    }
    
    /**
     * 获取日期的格式化字符串
     */
    public String getFormattedDate() {
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy年MM月dd日", java.util.Locale.getDefault());
        return formatter.format(selectedDate.getValue());
    }
    
    /**
     * 获取日期的简短格式化字符串
     */
    public String getShortFormattedDate() {
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("MM-dd", java.util.Locale.getDefault());
        return formatter.format(selectedDate.getValue());
    }
    
    /**
     * 获取数据库格式的日期字符串
     */
    public String getDatabaseFormattedDate() {
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM-dd", java.util.Locale.getDefault());
        return formatter.format(selectedDate.getValue());
    }
    
    /**
     * 获取自定义格式的选择日期字符串
     */
    public String getFormattedSelectedDate(String pattern) {
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(pattern, java.util.Locale.getDefault());
        return formatter.format(selectedDate.getValue());
    }
} 